package cn.micro.system.manage.controller;


import cn.micro.system.manage.common.exception.SystemException;
import cn.micro.system.manage.common.log.annotation.Log;
import cn.micro.system.manage.common.log.enums.BusinessType;
import cn.micro.system.manage.common.result.Result;
import cn.micro.system.manage.entity.AssetOdsFileRecordInfo;
import cn.micro.system.manage.service.AssetOdsFileRecordInfoService;
import cn.micro.system.manage.service.OssService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Paths;

/**
 * <p>
 * 文件信息表 前端控制器
 * </p>
 *
 * @author wr
 * @since 2024-01-05
 */
@RestController
@RequestMapping("/manage/assetOdsFileRecordInfo")
@RequiredArgsConstructor
@Slf4j
public class AssetOdsFileRecordInfoController {

    private final OssService ossService;


    @PostMapping("upload/{bucketName}")
    @Log(title = "文件管理--上传文件", businessType = BusinessType.UPLOAD)
    public Object upload(MultipartFile[] files, @PathVariable String bucketName){
        if (files==null || files.length == 0){
            return Result.fail().message("文件不能为空");
        }
        return Result.success(ossService.upload(files,bucketName));
    }

    @GetMapping("findView/{id}")
    public void findView(@PathVariable Long id, HttpServletResponse response){
        ossService.findView(id,response);
    }

    @GetMapping("download/{id}")
    @Log(title = "文件管理--下载文件", businessType = BusinessType.DOWNLOAD)
    public void download(@PathVariable Long id, HttpServletRequest request,HttpServletResponse response){
        try {
            ossService.download(id,null,request,response);
        } catch (UnsupportedEncodingException e) {
            throw new SystemException("下载失败");
        }
    }


}
